package com.mihong.handler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mihong.entity.Frame;
import com.mihong.entity.Message;
import com.mihong.entity.ServerConfig;
import com.mihong.util.JedisUtil;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.util.internal.StringUtil;
import redis.clients.jedis.Jedis;

/**
 * 	接收网关帧
 * @date: 2019年3月5日
 */
public class ReceiveHandler {

	private Jedis jedis = JedisUtil.getResource();
	
	/**
	 * 	注册包数据处理
	 * @param dataBody
	 * @param success
	 * @param frame
	 */
	public void registerHandler(String dataBody,Boolean success,Frame frame) {
		try {
			if(!success) 
				return;
			
			if(!StringUtil.isNullOrEmpty(dataBody)) {
				Map<String, Object> data = frame.getDataBody();
				data.put("dataBody", dataBody);
			}
			
			String frameJson = JSON.toJSONString(frame);
			jedis.hset("GateWayServer", "register", frameJson);
		} catch (Exception e) {
			 
		}finally {
			jedis.close();
		}
		
	}
	
	/**
	 * 	心跳包数据处理
	 * @param dataBody
	 * @param success
	 * @param frame
	 */
	public void heartBeatHandler(String dataBody,Boolean success,Frame frame) {
		
		try {
			if(!success) 
				return;
			
			if(!StringUtil.isNullOrEmpty(dataBody)) {
				Map<String, Object> data = frame.getDataBody();
				data.put("dataBody", dataBody);
			}
			
			String frameJson = JSON.toJSONString(frame);
			jedis.hset("GateWayServer", "heartBeat", frameJson);
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			jedis.close();
		}
		
	}
	
	/**
	 * 	终端状态包数据处理：网关发送注册包，服务器应答之后，获取终端状态数据帧，网关设备mac存于此数据帧
	 * @param dataBody
	 * @param success
	 * @param frame
	 * @return
	 */
	public String statusHandler(String dataBody,Boolean success,Frame frame) {
		
		StringBuilder equimentsMAC = new StringBuilder();//所有设备mac地址
		
		try {
			if(!success) 
				return null;
			
			if(!StringUtil.isNullOrEmpty(dataBody)) {
				Map<String, Object> map = frame.getDataBody();
				
				int count = dataBody.length()/Message.LENGTH_STATUS_ONE_EQUIPMENT;
				
				for(int i=0,j=1;i<count;i++,j++) {
					
		    		String one = dataBody.substring(i*Message.LENGTH_STATUS_ONE_EQUIPMENT, j*Message.LENGTH_STATUS_ONE_EQUIPMENT);
					String mac = one.substring(Message.START_INDEX, Message.LENGTH_STRING_HEX*Message.BYTE_MAC);
					String onLine = one.substring(Message.LENGTH_STRING_HEX*Message.BYTE_MAC, Message.LENGTH_STRING_HEX*(Message.BYTE_MAC+Message.BYTE_ONLINE));
					String mA = one.substring(Message.LENGTH_STRING_HEX*(Message.BYTE_MAC+Message.BYTE_ONLINE));
					
					List list = new ArrayList();
					list.add(onLine);
					list.add(mA);
					map.put(mac, list);
					
					//获取设备mac  
					equimentsMAC.append(mac);
					
				}
				
				frame.setDataBody(map);
			}
			
			String frameJson = JSON.toJSONString(frame);
			jedis.hset("GateWayServer", "status", frameJson);
			
		} catch (Exception e) {
			
		}finally {
			jedis.close();
		}
		
		return equimentsMAC.toString();
		
	}
	
	/**
	 * 	设备列表包数据处理
	 * @param dataBody
	 * @param success
	 * @param frame
	 */
	public void equipmentListHandler(String dataBody,Boolean success,Frame frame) {
		
		try {
			if(!success) 
				return;
			
			if(!StringUtil.isNullOrEmpty(dataBody)) {
				Map<String, Object> map = frame.getDataBody();
				
				map.put("success", dataBody);
				frame.setDataBody(map);
				
			}
			String frameJson = JSON.toJSONString(frame);
			jedis.hset("GateWayServer", "equipmentList", frameJson);
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			jedis.close();
		}
		
	}
	
	//拼接数据帧
//	frameStr.append(frame.getVersion()+frame.getFrameNo()+frame.getAddress()+frame.getCommand());
//	frameStr.append(Message.EQUIPMENT_DATA_LENGTH);//数据长度
//	if(success) {
//		frameStr.append(Message.SUCCESS);
//	}else {
//		frameStr.append(Message.FAILURE);
//	}
//	byte[] hexString = HexUtil.hexStringToBytes(frameStr.toString());
//	String crc = CRC16Util.CRC16_ccitt(hexString);
//	frameStr.append(crc+Message.FRAME_END);
//	frameStr.insert(0, Message.FRAME_HEAD);
//	return HexUtil.hexStringToBytes(frameStr.toString());

	/**
	 * 	ip切换包数据处理
	 * @param dataBody
	 * @param success
	 * @param frame
	 */
	public void ipSwitchingHandler(String dataBody,Boolean success,Frame frame) {
		
		try {
			if(!success) 
				return;
			
			if(!StringUtil.isNullOrEmpty(dataBody)) {
				Map<String, Object> map = frame.getDataBody();
				
				map.put("success", dataBody);
				frame.setDataBody(map);
			}
			
			String frameJson = JSON.toJSONString(frame);
			jedis.hset("GateWayServer", "ipSwitching", frameJson);	
				
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			jedis.close();
		}
		
	}


}
